﻿using System;
using System.Buffers.Binary;
using static DataCapture.Helper.PublicHelp;
using System.Reflection;
namespace DataCapture.Helper
{
   

    public enum PlcDataType
    {
        Byte = 8,
        Short = 16,
        Int = 32
    }

    public static class PlcDataHelper
    {
        private  static int startAddress { get; set; } = 1000; //默认D1100的起始

        private static int GetOffset(int address)
        {
            return address - startAddress; // D1000的起始地址为0
        }
        private static int GetOffset(int address ,int minAddress)
        {
            return address - minAddress;
        }
        public static byte GetByteValue(byte[] data, int address,int minAddress)
        {
            int offset = GetOffset(address, minAddress);
            return data[offset];
        }
        public static ushort GetUShortValue(byte[] data, int address, int minAddress)
        {
            if (data == null) return 0;
            if (address < minAddress || address + 1 >= data.Length) return 0;

            try
            {
                int offset = GetOffset(address, minAddress);
                Span<byte> bytes = data.AsSpan(offset, 2);

                if (BitConverter.IsLittleEndian)
                {
                    return BinaryPrimitives.ReadUInt16LittleEndian(bytes);
                }
                else
                {
                    return BinaryPrimitives.ReadUInt16BigEndian(bytes);
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.Info($"PlcDataHelper.GetUShortValue 错误内容：{ex.Message}");
                return 0; // 返回 null 表示错误
            }
        }
        public static ushort GetUShortValue(ushort[] data, int address, int minAddress)
        {
            if (data == null) return 0;
            if (address < minAddress || address + 1 >= data.Length) return 0;

            try
            {
                int offset = GetOffset(address, minAddress);
                if (offset < 0 || offset >= data.Length)
                {
                    throw new ArgumentOutOfRangeException($"地址{address}超出范围");
                }
                // 4. 返回对应数据
                return data[offset];
            }
            catch (Exception ex)
            {
                Log4NetHelper.Info($"PlcDataHelper.GetUShortValue 错误内容：{ex.Message}");
                return 0; // 返回 null 表示错误
            }
        }
        public static short GetShortValue(byte[] data, int address, int minAddress)
        {
            try
            {
                int offset = GetOffset(address, minAddress);
                if (BitConverter.IsLittleEndian)
                    return BitConverter.ToInt16(data, offset);
                else
                    return BitConverter.ToInt16(new byte[] { data[offset + 1], data[offset] }, 0);
            }
            catch (Exception ex)
            {
                Log4NetHelper.Info($"PlcDataHelper.GetShortValue错误内容：{ex.Message}");
                return -1;
            }
           
        }

        public static int GetIntValue(byte[] data, int address)
        {
            int offset = GetOffset(address);
            if (BitConverter.IsLittleEndian)
                return BitConverter.ToInt32(data, offset);
            else
                return BitConverter.ToInt32(new byte[] { data[offset + 3], data[offset + 2], data[offset + 1], data[offset] }, 0);
        }

        public static object GetValue(byte[] data, int address, PlcDataType type)
        {
            switch (type)
            {
                case PlcDataType.Byte:
                    return GetByteValue(data, address,startAddress);
                case PlcDataType.Short:
                    return GetShortValue(data, address, startAddress);
                case PlcDataType.Int:
                    return GetIntValue(data, address);
                default:
                    throw new ArgumentOutOfRangeException(nameof(type));
            }
        }
    }
}